package com.example.demo.service;/**
 * 类描述
 *
 * @author 8522
 * @date 2021/10/9 10:57
 * 版权所有 Copyright www.dahantc.com
 */

import com.example.demo.dto.SysJobPO;
import com.example.demo.enums.SysJobStatus;
import com.example.demo.util.CronTaskRegistrar;
import com.example.demo.util.SchedulingRunnable;
import com.example.demo.util.springUtil.OperationResUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 *@Description:
 *@Author: 8522
 *@Date: 2021/10/9  10:57
 *@Version v1.0
 */

@Service
public class SysJob {

    @Autowired
    private CronTaskRegistrar cronTaskRegistrar;

    @Autowired
    private SysJobRepository sysJobRepository;

    public String addJob(SysJobPO sysJob) {
        boolean success = sysJobRepository.addSysJob(sysJob);
        if (!success) {
            return OperationResUtils.fail("新增失败");
        } else {
            if (sysJob.getJobStatus().equals(SysJobStatus.正常.ordinal())) {
                SchedulingRunnable task = new SchedulingRunnable(sysJob.getBeanName(), sysJob.getMethodName(), sysJob.getMethodParams());
                cronTaskRegistrar.addCronTask(task, sysJob.getCronExpression());
            }
        }
        return OperationResUtils.success("定时任务增加");
    }

    /**
     * 方法描述 修改定时任务
     *
     * @param
     * @param
     * @return {@linkjava.lang.String}
     * @throws
     * @date 2021/10/8 19:35
     */
    public String editJob(SysJobPO sysJob) {
        boolean success = sysJobRepository.editSysJob(sysJob);
        if (!success) {
            return OperationResUtils.fail("编辑失败");
        } else {
            //先移除再添加
            if (sysJob.getJobStatus().equals(SysJobStatus.正常.ordinal())) {
                SchedulingRunnable task = new SchedulingRunnable(sysJob.getBeanName(), sysJob.getMethodName(), sysJob.getMethodParams());
                cronTaskRegistrar.removeCronTask(task);
            }

            if (sysJob.getJobStatus().equals(SysJobStatus.正常.ordinal())) {
                SchedulingRunnable task = new SchedulingRunnable(sysJob.getBeanName(), sysJob.getMethodName(), sysJob.getMethodParams());
                cronTaskRegistrar.addCronTask(task, sysJob.getCronExpression());
            }
        }

        return OperationResUtils.success();
    }


    public String delJob(SysJobPO sysJob) {
        boolean success = sysJobRepository.deleteSysJobById(sysJob.getJobId());
        if (!success) {
            return OperationResUtils.fail("删除失败");
        } else {
            if (sysJob.getJobStatus().equals(SysJobStatus.正常.ordinal())) {
                SchedulingRunnable task = new SchedulingRunnable(sysJob.getBeanName(), sysJob.getMethodName(), sysJob.getMethodParams());
                cronTaskRegistrar.removeCronTask(task);
            }
        }

        return OperationResUtils.success();
    }

    public String changeJobStatue(SysJobPO sysJob) {
        if (sysJob.getJobStatus().equals(SysJobStatus.正常.ordinal())) {
            SchedulingRunnable task = new SchedulingRunnable(sysJob.getBeanName(), sysJob.getMethodName(), sysJob.getMethodParams());
            cronTaskRegistrar.addCronTask(task, sysJob.getCronExpression());
        } else {
            SchedulingRunnable task = new SchedulingRunnable(sysJob.getBeanName(), sysJob.getMethodName(), sysJob.getMethodParams());
            cronTaskRegistrar.removeCronTask(task);
        }
        return OperationResUtils.success();
    }

}
